<?php
/**
 * 异常和错误处理具柄类
 * User: 苏近之
 * Date: 2019-03-23
 * Time: 20:24
 */

namespace app\common\base;

use app\common\enum\HttpResponseStatusCode;
use Exception;
use think\exception\Handle;

class ExceptionErrorHandler extends Handle
{

    /**
     * 渲染错误和异常
     * @param Exception $e
     * @return \think\Response|\think\response\Json
     */
    public function render(Exception $e)
    {
        // 自定义 API 异常错误处理
        if($e instanceof ApiException){
            return json(['code' => $e->getCode(), 'message' => $e->getMessage()],
                HttpResponseStatusCode::INTERNAL_SERVER_ERROR);
        }

        // 其他错误交给系统处理
        return parent::render($e);
    }
}